/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package assignment4;

import java.util.ArrayList;
import java.util.Random;

/**
 *
 * @author chrisjaramillo
 */
public class HiddenNode {
    NeuralNetwork m_network;
    ArrayList m_inputNodes;
    ArrayList m_weights;
    double m_value;
    double m_threshold;
    double m_input;
    
    HiddenNode()
    {
        init();
    }
    
    HiddenNode(NeuralNetwork network)
    {
        init();
        m_network = network;
    }
    
    private void init()
    {
        m_inputNodes = new ArrayList();
        m_weights = new ArrayList();
        m_value = 0.0;
        m_threshold = 0.0;
        m_input = 0.0;
    }
    
    public void setRandomWeights()
    {
        Random random = new Random();
        System.out.println("Hidden node");
        for(int i=0; i<m_network.outputNodes().size(); i++)
        {
            double val = random.nextDouble();
            val *= 4.8;
            val -= 2.4;
            m_weights.add(val);
            System.out.println("Weight " + i + ": " + val);
        }
        double val = random.nextDouble();
        val *= 4.8;
        val -= 2.4;
        m_threshold = val;
    }
    
    public void value(int nodeNumber)
    {
        ArrayList inputNodes = m_network.inputNodes();
        double value = 0.0;
        for(int i = 0; i<inputNodes.size(); i++)
        {
            InputNode node = (InputNode)inputNodes.get(i);
            double inputNodeVal = node.value();
            double weight = node.weight(nodeNumber);
            value += inputNodeVal * weight - m_threshold;
        }
        m_input = value;
        m_value = 1/(1+Math.pow(Math.E,(-1 * value)));
    }
    
    public double value()
    {
        return m_value;
    }
    
    public double weight(int index)
    {
        return (double)m_weights.get(index);
    }
}
